package cn.melon.view;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 * @ClassName: JImgPanel
 * @author Hzy
 * 继承自JPanel的类，可以通过传入图片路径方便地设置背景图像
 */
public class JImgPanel extends JPanel{
	private String imgPath;
	private int height;
	private int width;
	public JImgPanel(Dimension s,String imgPath){
	    this.width = s.width;
	    this.height = s.height;
	    this.imgPath = imgPath;
	    this.setPreferredSize(s);
	}
	public JImgPanel(String imgPath,int width,int height){
		super();
		this.width = width;
		this.height = height;
		this.imgPath = imgPath;
		this.setPreferredSize(new Dimension(width,height));
	}
	
	@Override
	public void paintComponent(Graphics g){
		BufferedImage icon = new BufferedImage(width,height,BufferedImage.TYPE_4BYTE_ABGR);
		try{
			//icon = ImageIO.read(new File(imgPath));
			icon = ImageIO.read(this.getClass().getResource(imgPath));
			g.drawImage(icon,0,0,width,height,null);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public String getBacImgPath(){
	    return imgPath;
	}
	public void setBackgroundImg(String imgPath){
	    this.imgPath = imgPath;
	    this.repaint();
	}

}
